
#import "UIOpenGLView.h"

#define CAMERA_RADIUS		320

@implementation UIOpenGLView

@synthesize	camera,renderer;
@dynamic cameraAngleVert,cameraAngleHoriz;
@dynamic cameraCenter,cameraRadius;
@synthesize cameraControlEnabled;

+ (Class) layerClass
{
	return [CAEAGLLayer class];
}

- (id)initWithCoder:(NSCoder*)coder
{
	if((self = [super initWithCoder:coder])) 
	{	
		// Get the layer
		CAEAGLLayer *eaglLayer = (CAEAGLLayer*) self.layer;
		
		eaglLayer.opaque = YES;
		eaglLayer.drawableProperties = [NSDictionary dictionaryWithObjectsAndKeys:
										[NSNumber numberWithBool:FALSE], kEAGLDrawablePropertyRetainedBacking, kEAGLColorFormatRGBA8, kEAGLDrawablePropertyColorFormat, nil];
		
		renderer = [Renderer alloc];
		[renderer initWithLayer:eaglLayer];
		
		camera = [Camera alloc];
		[camera initWithPerspectiveFov:M_PI/4 aspectRatio:((float)renderer.backingWidth)/((float)renderer.backingHeight) zNear:0.1 zFar:1000];
		[camera setViewPortX:0 Y:0 W:renderer.backingWidth H:renderer.backingHeight];

		self.cameraCenter = Vector3Make(128, 128, 0);
		self.cameraRadius = CAMERA_RADIUS;
		
//		self.cameraAngleHoriz = -M_PI;
//		self.cameraAngleVert = -M_PI/4;
		self.cameraAngleHoriz = -M_PI/2;
		self.cameraAngleVert = M_PI/4;
		
//		[camera setPosX:cameraCenter.x+cameraRadius*sinf(cameraAngleHoriz) Y:cameraCenter.y-cameraRadius*cosf(cameraAngleHoriz) Z:-cameraRadius*cosf(cameraAngleVert)];
//		[camera lookAtPos:&cameraCenter];

		cameraControlEnabled = YES;
		
		[renderer setCamera:camera];
		
		[self drawView];
	}
	
	return self;
}


- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // Initialization code
    }
    return self;
}


-(void)drawRect:(CGRect)rect
{
//	[super drawRect:rect];
}

- (void)dealloc 
{
	[camera release];
	[renderer release];
	
    [super dealloc];
}

- (void)layoutSubviews 
{
	[renderer onFrameResize];
	[self drawView];
}

- (void)drawView
{
//	[renderer setClearColor:ColorRGBAMake(0.6, 0.6, 0, 1)];
//	[renderer drawBegin];
//	
//		[renderer disableFaceCulling];
//		[renderer enableTextures];
//	
//	[renderer drawEnd];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	if(!cameraControlEnabled)
		return;
	
	NSArray *allTouches = [touches allObjects];

	UITouch *touch = [allTouches objectAtIndex:0];

	CGPoint pos = [touch locationInView:touch.view];
	CGPoint prevPos = [touch previousLocationInView:touch.view];

	
	if(touches.count>=2)
	{
		UITouch *touch2 = [allTouches objectAtIndex:1];

		CGPoint pos2 = [touch2 locationInView:touch2.view];
		CGPoint prevPos2 = [touch2 previousLocationInView:touch2.view];
		
//		float prevDist = sqrt((pos2.x - prevPos2.x)*(pos2.x - prevPos2.x) + (pos2.y - prevPos2.y)*(pos2.y - prevPos2.y));
		float prevDist = sqrt((prevPos2.x - prevPos.x)*(prevPos2.x - prevPos.x) + (prevPos2.y - prevPos.y)*(prevPos2.y - prevPos.y));		
		float curDist = sqrt((pos2.x - pos.x)*(pos2.x - pos.x) + (pos2.y - pos.y)*(pos2.y - pos.y));		
		self.cameraRadius = cameraRadius + (prevDist - curDist)*0.4f;
	}
	else
	{	
		self.cameraAngleHoriz = cameraAngleHoriz - (pos.x - prevPos.x)*0.03;
		self.cameraAngleVert  = cameraAngleVert + (pos.y - prevPos.y)*0.01;
	}

	[camera setPosX:cameraCenter.x+cameraRadius*cosf(cameraAngleHoriz) Y:cameraCenter.y-cameraRadius*sinf(cameraAngleHoriz) Z:-cameraRadius*sinf(cameraAngleVert)];
	[camera lookAtPos:&cameraCenter];
}

-(void) setCameraRadius:(float) radius
{
	if(radius < 10)
		radius = 10;
	else if(radius > 1500)
		radius = 1500;
	
	cameraRadius = radius;
	
	[camera setPosX:cameraCenter.x+cameraRadius*sinf(cameraAngleHoriz) Y:cameraCenter.y-cameraRadius*cosf(cameraAngleHoriz) Z:-cameraRadius*cosf(cameraAngleVert)];
	[camera lookAtPos:&cameraCenter];	
}

-(float) cameraRadius
{
	return cameraRadius;
}

-(void) setCameraCenter:(Vector3) center
{
	cameraCenter = center;
	
//	[camera setPosX:cameraCenter.x+cameraRadius*sinf(cameraAngleHoriz) Y:cameraCenter.y-cameraRadius*cosf(cameraAngleHoriz) Z:-cameraRadius*cosf(cameraAngleVert)];
	[camera setPosX:cameraCenter.x+cameraRadius*cosf(cameraAngleHoriz) Y:cameraCenter.y-cameraRadius*sinf(cameraAngleHoriz) Z:-cameraRadius*sinf(cameraAngleVert)];
	[camera lookAtPos:&cameraCenter];	
}

-(Vector3) cameraCenter
{
	return cameraCenter;
}

-(void) setCameraAngleVert:(float)angle
{
	if(angle<0.2)
		angle = 0.2;
	else if(angle > M_PI/2.5)
		angle = M_PI/2.5;

	cameraAngleVert = angle;

//	[camera setPosX:cameraCenter.x+cameraRadius*sinf(cameraAngleHoriz) Y:cameraCenter.y-cameraRadius*cosf(cameraAngleHoriz) Z:-cameraRadius*cosf(cameraAngleVert)];
	[camera setPosX:cameraCenter.x+cameraRadius*cosf(cameraAngleHoriz) Y:cameraCenter.y-cameraRadius*sinf(cameraAngleHoriz) Z:-cameraRadius*sinf(cameraAngleVert)];
	[camera lookAtPos:&cameraCenter];	
}

-(float) cameraAngleHoriz
{
	return cameraAngleHoriz;
}

-(void) setCameraAngleHoriz:(float)angle
{
	cameraAngleHoriz = angle;
	
//	[camera setPosX:cameraCenter.x+cameraRadius*sinf(cameraAngleHoriz) Y:cameraCenter.y-cameraRadius*cosf(cameraAngleHoriz) Z:-cameraRadius*cosf(cameraAngleVert)];
	[camera setPosX:cameraCenter.x+cameraRadius*cosf(cameraAngleHoriz) Y:cameraCenter.y-cameraRadius*sinf(cameraAngleHoriz) Z:-cameraRadius*sinf(cameraAngleVert)];
	[camera lookAtPos:&cameraCenter];	
}

-(float) cameraAngleVert
{
	return cameraAngleVert;
}

@end
